#include "spk_camera.h"
#include "utils.h"
#include "config.h"

#include "iniparser.h"

#define LOG_TAG "ini_config"
#define LOG_LVL LOG_LVL_INFO
#include "spk_ulog.h"

void create_example_ini_file(void)
{
    FILE *ini;

    if ((ini = fopen("example.ini", "w")) == NULL)
    {
        fprintf(stderr, "iniparser: cannot create example.ini\n");
        return;
    }

    fprintf(ini,
            "#\n"
            "# 城堪科技摄像头配置文件，谨慎修改！\n"
            "#\n"
            "\n"
            "[系统配置]\n"
            "\n"
            "设备名称      = 测试设备 ;\n"
            "平台接入方式  = 28181 ;\n"
            "\n"
            "[网络配置]\n"
            "\n"
            "网卡          = 有线网卡 ;\n"
            "DHCP          = yes ;\n"
            "IP地址        = 192.168.31.24 ;\n"
            "子网掩码      = 255.255.255.0 ;\n"
            "网关地址      = 192.168.31.1 ;\n"
            "首选DNS服务器 = 8.8.8.8 ;\n"
            "备用DNS服务器 = 4.4.4.2 ;\n"
            "\n"
            "[SIP配置]\n"
            "\n"
            "连接状态    = no ;\n"
            "协议版本    = GB/T28181-2016 ;\n"
            "本地端口    = 8899 ;\n"
            "传输协议    = TCP ;\n"
            "服务器ID    = 34020000001300000001  ;\n"
            "服务器域    = 3402000000  ;\n"
            "服务器地址  = 106.14.80.181  ;\n"
            "服务器端口  = 30905  ;\n"
            "用户名      = 34020000001320000009  ;\n"
            "用户认证ID  = 34020000001320000009  ;\n"
            "密码        = 123456  ;\n"
            "注册有效期  = 3600  ;\n"
            "\n"
            "[视频配置]\n"
            "\n"
            "视频宽度    = 640 ;\n"
            "视频高度    = 480 ;\n"
            "视频码率    = 500 ;\n"
            "视频帧率    = 25  ;\n"
            "\n"
            "[OSD配置]\n"
            "\n"
            "通道名称     = 测试设备 ;\n"
            "显示通道名称 = yes ;\n"
            "显示日期     = yes ;\n"
            "显示时间     = yes  ;\n"
            "时间格式     = 12小时制  ;\n"
            "日期格式     = 年年-月月-日日  ;\n"
            "叠加信息1    = 测试信息1  ;\n"
            "叠加信息2    = 测试信息2  ;\n"
            "叠加信息3    = 测试信息3  ;\n"
            "叠加信息4    = 测试信息4  ;\n"
            "叠加信息1坐标x    =    0   ;\n"
            "叠加信息1坐标y    =    0   ;\n"
            "叠加信息2坐标x    =    0   ;\n"
            "叠加信息2坐标y    =    0   ;\n"
            "叠加信息3坐标x    =    0   ;\n"
            "叠加信息3坐标y    =    0   ;\n"
            "叠加信息4坐标x    =    0   ;\n"
            "叠加信息4坐标y    =    0   ;\n"
            "\n"
            "[抓拍配置]\n"
            "\n"
            "是否启用抓拍          = no ;\n"
            "图片格式             = BMP ;\n"
            "抓拍间隔             = 2000  ;\n" // 存储的时候都是用ms来存
            "抓拍间隔时间单位      = 毫秒  ;\n"
            "抓图是否保存到服务器  = yes  ;\n"
            "上传地址              = http://106.14.80.181:30019/api/file/Uploader/jpg ;\n"
            "周一是否抓拍          = no ;\n"
            "周一抓拍起始时间      = 0  ;\n"
            "周一抓拍结束时间      = 24 ;\n"
            "周二是否抓拍          = no ;\n"
            "周二抓拍起始时间      = 0  ;\n"
            "周二抓拍结束时间      = 24 ;\n"
            "周三是否抓拍          = no ;\n"
            "周三抓拍起始时间      = 0  ;\n"
            "周三抓拍结束时间      = 24 ;\n"
            "周四是否抓拍          = no ;\n"
            "周四抓拍起始时间      = 0  ;\n"
            "周四抓拍结束时间      = 24 ;\n"
            "周五是否抓拍          = no ;\n"
            "周五抓拍起始时间      = 0  ;\n"
            "周五抓拍结束时间      = 24 ;\n"
            "周六是否抓拍          = no ;\n"
            "周六抓拍起始时间      = 0  ;\n"
            "周六抓拍结束时间      = 24 ;\n"
            "周天是否抓拍          = no ;\n"
            "周天抓拍起始时间      = 0  ;\n"
            "周天抓拍结束时间      = 24 ;\n"
            "\n"
            "[录像配置]\n"
            "\n"
            "是否启用录像          = no ;\n"
            "周一是否录像          = no ;\n"
            "周一录像起始时间      = 0  ;\n"
            "周一录像结束时间      = 24 ;\n"
            "周二是否录像          = no ;\n"
            "周二录像起始时间      = 0  ;\n"
            "周二录像结束时间      = 24 ;\n"
            "周三是否录像          = no ;\n"
            "周三录像起始时间      = 0  ;\n"
            "周三录像结束时间      = 24 ;\n"
            "周四是否录像          = no ;\n"
            "周四录像起始时间      = 0  ;\n"
            "周四录像结束时间      = 24 ;\n"
            "周五是否录像          = no ;\n"
            "周五录像起始时间      = 0  ;\n"
            "周五录像结束时间      = 24 ;\n"
            "周六是否录像          = no ;\n"
            "周六录像起始时间      = 0  ;\n"
            "周六录像结束时间      = 24 ;\n"
            "周天是否录像          = no ;\n"
            "周天录像起始时间      = 0  ;\n"
            "周天录像结束时间      = 24 ;\n"
            "\n");
    fclose(ini);
}

int init_file_encode(std::string file_name, struct device_info *info)
{
    // gb2312--utf-8
    // info->device_name;
    // info->osd_channel_name;
    // info->osd_info;
    gb2312_to_utf8(info->device_name);
    gb2312_to_utf8(info->osd_channel_name);
    for (int i = 0; i < 4; ++i)
    {
        gb2312_to_utf8(info->osd_info[i]);
    }

    FILE *ini;

    if ((ini = fopen(file_name.c_str(), "wt+")) == NULL)
    {
        LOG_E("[%s] line:%d faild to create ini file(%s)!\n", __FUNCTION__, __LINE__, file_name.c_str());
        return -1;
    }

    fprintf(ini, "#\n");
    fprintf(ini, "# 城堪科技摄像头配置文件，谨慎修改！\n");
    fprintf(ini, "#\n");
    fprintf(ini, "\n");
    fprintf(ini, "[系统配置]\n");
    fprintf(ini, "\n");
    fprintf(ini, "设备名称      = %s ;\n", info->device_name);
    fprintf(ini, "平台接入方式  = %s ;\n", "28181");
    fprintf(ini, "\n");
    fprintf(ini, "[网络配置]\n");
    fprintf(ini, "\n");
    fprintf(ini, "网卡          = %s ;\n", (info->net_card_type == NET_CARD_WIRED) ? "有线网卡" : ((info->net_card_type == NET_CARD_WIFI) ? "WIFI" : "4G模块"));
    fprintf(ini, "DHCP          = %s ;\n", info->auto_ip_addr ? "yes" : "no");
    fprintf(ini, "IP地址        = %s ;\n", ip_addr_trans(info->ip_addr).c_str());
    fprintf(ini, "子网掩码      = %s ;\n", ip_addr_trans(info->sub_net_mask).c_str());
    fprintf(ini, "网关地址      = %s ;\n", ip_addr_trans(info->gate_way).c_str());
    fprintf(ini, "首选DNS服务器 = %s ;\n", ip_addr_trans(info->major_dns).c_str());
    fprintf(ini, "备用DNS服务器 = %s ;\n", ip_addr_trans(info->minor_dns).c_str());
    fprintf(ini, "\n");
    fprintf(ini, "[SIP配置]\n");
    fprintf(ini, "\n");
    fprintf(ini, "连接状态    = %s ;\n", info->sip_server_connect ? "yes" : "no");
    fprintf(ini, "协议版本    = %s ;\n", "GB/T28181-2016");
    fprintf(ini, "本地端口    = %d ;\n", info->sip_local_port);
    fprintf(ini, "传输协议    = %s ;\n", info->sip_protocal_type == SIP_PROTOCAL_UDP ? "UDP" : "TCP");
    fprintf(ini, "服务器ID    = %s  ;\n", info->sip_server_id);
    fprintf(ini, "服务器域    = %s  ;\n", info->sip_server_domain);
    fprintf(ini, "服务器地址  = %s ;\n", ip_addr_trans(info->sip_server_ip_addr).c_str());
    fprintf(ini, "服务器端口  = %d ;\n", info->sip_server_port);
    fprintf(ini, "用户名      = %s  ;\n", info->sip_user_name);
    fprintf(ini, "用户认证ID  = %s  ;\n", info->sip_user_id);
    fprintf(ini, "密码        = %s  ;\n", info->sip_password);
    fprintf(ini, "注册有效期  = %d  ;\n", info->sip_register_timeout_second);
    fprintf(ini, "\n");
    fprintf(ini, "[视频配置]\n");
    fprintf(ini, "\n");
    fprintf(ini, "视频宽度    = %d ;\n", info->video_iw);
    fprintf(ini, "视频高度    = %d ;\n", info->video_ih);
    fprintf(ini, "视频码率    = %d ;\n", info->video_encode_bps);
    fprintf(ini, "视频帧率    = %d ;\n", info->video_frame_rate);
    fprintf(ini, "\n");
    fprintf(ini, "[OSD配置]\n");
    fprintf(ini, "\n");
    fprintf(ini, "通道名称     = %s ;\n", info->osd_channel_name);
    fprintf(ini, "显示通道名称 = %s ;\n", info->osd_en_channel_name ? "yes" : "no");
    fprintf(ini, "显示日期     = %s ;\n", info->osd_en_date_info ? "yes" : "no");
    fprintf(ini, "显示时间     = %s ;\n", info->osd_en_week_info ? "yes" : "no");
    fprintf(ini, "时间格式     = 12小时制  ;\n");
    fprintf(ini, "日期格式     = 年年-月月-日日  ;\n");
    fprintf(ini, "叠加信息1    = %s ;\n", info->osd_info[0]);
    fprintf(ini, "叠加信息2    = %s ;\n", info->osd_info[1]);
    fprintf(ini, "叠加信息3    = %s ;\n", info->osd_info[2]);
    fprintf(ini, "叠加信息4    = %s ;\n", info->osd_info[3]);
    fprintf(ini, "叠加信息1坐标x    =    %d   ;\n", info->osd_info_location[0][0]);
    fprintf(ini, "叠加信息1坐标y    =    %d   ;\n", info->osd_info_location[0][1]);
    fprintf(ini, "叠加信息2坐标x    =    %d   ;\n", info->osd_info_location[1][0]);
    fprintf(ini, "叠加信息2坐标y    =    %d   ;\n", info->osd_info_location[1][1]);
    fprintf(ini, "叠加信息3坐标x    =    %d   ;\n", info->osd_info_location[2][0]);
    fprintf(ini, "叠加信息3坐标y    =    %d   ;\n", info->osd_info_location[2][1]);
    fprintf(ini, "叠加信息4坐标x    =    %d   ;\n", info->osd_info_location[3][0]);
    fprintf(ini, "叠加信息4坐标y    =    %d   ;\n", info->osd_info_location[3][1]);
    fprintf(ini, "\n");
    fprintf(ini, "[抓拍配置]\n");
    fprintf(ini, "\n");
    fprintf(ini, "是否启用抓拍          = %s ;\n", info->capture_en_start ? "yes" : "no");
    fprintf(ini, "图片格式             = %s ;\n", info->image_format == IMAGE_FORMAT_BMP ? "BMP" : "JPEG");
    fprintf(ini, "抓拍间隔             = %d  ;\n", info->capture_time_interval);
    fprintf(ini, "抓拍间隔时间单位      = %s  ;\n", (info->capture_time_unit == IMAGE_TIME_UNIT_MILLISECOND) ? "毫秒" : (info->capture_time_unit == IMAGE_TIME_UNIT_SECOND) ? "秒"
                                                                                                                    : (info->capture_time_unit == IMAGE_TIME_UNIT_MINUTE)   ? "分钟"
                                                                                                                    : (info->capture_time_unit == IMAGE_TIME_UNIT_HOUR)     ? "小时"
                                                                                                                                                                            : "天");
    fprintf(ini, "抓图是否保存到服务器  = %s  ;\n", info->capture_en_save_server ? "yes" : "no");
    fprintf(ini, "上传地址              = %s ;\n", info->capture_server_url);
    fprintf(ini, "周一是否抓拍          = %s ;\n", info->capture_every_week[0][0] ? "yes" : "no");
    fprintf(ini, "周一抓拍起始时间      = %d ;\n", info->capture_every_week[0][1]);
    fprintf(ini, "周一抓拍结束时间      = %d ;\n", info->capture_every_week[0][2]);
    fprintf(ini, "周二是否抓拍          = %s ;\n", info->capture_every_week[1][0] ? "yes" : "no");
    fprintf(ini, "周二抓拍起始时间      = %d ;\n", info->capture_every_week[1][1]);
    fprintf(ini, "周二抓拍结束时间      = %d ;\n", info->capture_every_week[1][2]);
    fprintf(ini, "周三是否抓拍          = %s ;\n", info->capture_every_week[2][0] ? "yes" : "no");
    fprintf(ini, "周三抓拍起始时间      = %d ;\n", info->capture_every_week[2][1]);
    fprintf(ini, "周三抓拍结束时间      = %d ;\n", info->capture_every_week[2][2]);
    fprintf(ini, "周四是否抓拍          = %s ;\n", info->capture_every_week[3][0] ? "yes" : "no");
    fprintf(ini, "周四抓拍起始时间      = %d ;\n", info->capture_every_week[3][1]);
    fprintf(ini, "周四抓拍结束时间      = %d ;\n", info->capture_every_week[3][2]);
    fprintf(ini, "周五是否抓拍          = %s ;\n", info->capture_every_week[4][0] ? "yes" : "no");
    fprintf(ini, "周五抓拍起始时间      = %d ;\n", info->capture_every_week[4][1]);
    fprintf(ini, "周五抓拍结束时间      = %d ;\n", info->capture_every_week[4][2]);
    fprintf(ini, "周六是否抓拍          = %s ;\n", info->capture_every_week[5][0] ? "yes" : "no");
    fprintf(ini, "周六抓拍起始时间      = %d ;\n", info->capture_every_week[5][1]);
    fprintf(ini, "周六抓拍结束时间      = %d ;\n", info->capture_every_week[5][2]);
    fprintf(ini, "周天是否抓拍          = %s ;\n", info->capture_every_week[6][0] ? "yes" : "no");
    fprintf(ini, "周天抓拍起始时间      = %d ;\n", info->capture_every_week[6][1]);
    fprintf(ini, "周天抓拍结束时间      = %d ;\n", info->capture_every_week[6][2]);
    fprintf(ini, "\n");
    fprintf(ini, "[录像配置]\n");
    fprintf(ini, "\n");
    fprintf(ini, "是否启用录像          = %s ;\n", info->record_en_start ? "yes" : "no");
    fprintf(ini, "周一是否录像          = %s ;\n", info->record_every_week[0][0] ? "yes" : "no");
    fprintf(ini, "周一录像起始时间      = %d ;\n", info->record_every_week[0][1]);
    fprintf(ini, "周一录像结束时间      = %d ;\n", info->record_every_week[0][2]);
    fprintf(ini, "周二是否录像          = %s ;\n", info->record_every_week[1][0] ? "yes" : "no");
    fprintf(ini, "周二录像起始时间      = %d ;\n", info->record_every_week[1][1]);
    fprintf(ini, "周二录像结束时间      = %d ;\n", info->record_every_week[1][2]);
    fprintf(ini, "周三是否录像          = %s ;\n", info->record_every_week[2][0] ? "yes" : "no");
    fprintf(ini, "周三录像起始时间      = %d ;\n", info->record_every_week[2][1]);
    fprintf(ini, "周三录像结束时间      = %d ;\n", info->record_every_week[2][2]);
    fprintf(ini, "周四是否录像          = %s ;\n", info->record_every_week[3][0] ? "yes" : "no");
    fprintf(ini, "周四录像起始时间      = %d ;\n", info->record_every_week[3][1]);
    fprintf(ini, "周四录像结束时间      = %d ;\n", info->record_every_week[3][2]);
    fprintf(ini, "周五是否录像          = %s ;\n", info->record_every_week[4][0] ? "yes" : "no");
    fprintf(ini, "周五录像起始时间      = %d ;\n", info->record_every_week[4][1]);
    fprintf(ini, "周五录像结束时间      = %d ;\n", info->record_every_week[4][2]);
    fprintf(ini, "周六是否录像          = %s ;\n", info->record_every_week[5][0] ? "yes" : "no");
    fprintf(ini, "周六录像起始时间      = %d ;\n", info->record_every_week[5][1]);
    fprintf(ini, "周六录像结束时间      = %d ;\n", info->record_every_week[5][2]);
    fprintf(ini, "周天是否录像          = %s ;\n", info->record_every_week[6][0] ? "yes" : "no");
    fprintf(ini, "周天录像起始时间      = %d ;\n", info->record_every_week[6][1]);
    fprintf(ini, "周天录像结束时间      = %d ;\n", info->record_every_week[6][2]);
    fprintf(ini, "\n");
    fclose(ini);

    return 0;
}

int init_file_decode(std::string file_name, struct device_info *info)
{
    dictionary *ini = iniparser_load(file_name.c_str());

    if (ini == NULL)
    {
        LOG_E("[%s] line:%d faild to load ini file(%s)!\n", __FUNCTION__, __LINE__, file_name.c_str());
        return -1;
    }
    // iniparser_dump(ini, stderr);

    memset(info, 0, sizeof(struct device_info));

    /*解析设备名称*/
    const char *str = nullptr;
    str = iniparser_getstring(ini, "系统配置:设备名称", "未命名");
    memcpy(info->device_name, str, strlen(str));

    /*解析平台接入方式*/
    str = iniparser_getstring(ini, "系统配置:平台接入方式", "28181");

    if (string(str) == "28181")
    {
        info->platform_type = PLATFORM_GB28181;
    }
    else
    {
        LOG_E("不支持的平台类型!");
    }

    /*解析网卡类型*/
    str = iniparser_getstring(ini, "网络配置:网卡", "有线网卡");

    if (string(str) == "有线网卡")
    {
        info->net_card_type = NET_CARD_WIRED;
    }
    else if (string(str) == "4G模块")
    {
        info->net_card_type = NET_CARD_4G_MODULE;
    }
    else if (string(str) == "WIFI")
    {
        info->net_card_type = NET_CARD_WIFI;
    }
    else
    {
        LOG_E("不支持的网卡类型!");
    }

    str = iniparser_getstring(ini, "网络配置:DHCP", "no");
    info->auto_ip_addr = (string(str) == "yes") ? 1 : 0;

    str = iniparser_getstring(ini, "网络配置:IP地址", "192.168.31.24");
    ip_addr_trans(string(str), info->ip_addr);

    str = iniparser_getstring(ini, "网络配置:子网掩码", "255.255.255.0");
    ip_addr_trans(string(str), info->sub_net_mask);

    str = iniparser_getstring(ini, "网络配置:网关地址", "192.168.31.1");
    ip_addr_trans(string(str), info->gate_way);

    str = iniparser_getstring(ini, "网络配置:首选DNS服务器", "8.8.8.8");
    ip_addr_trans(string(str), info->major_dns);

    str = iniparser_getstring(ini, "网络配置:备用DNS服务器", "4.4.4.2");
    ip_addr_trans(string(str), info->minor_dns);

    str = iniparser_getstring(ini, "SIP配置:连接状态", "no");
    info->sip_server_connect = (string(str) == "yes") ? 1 : 0;

    str = iniparser_getstring(ini, "SIP配置:协议版本", "GB/T28181-2016");

    if (string(str) == "GB/T28181-2016")
    {
        info->sip_protocal_version = SIP_VERSION_GBT28181_2016;
    }
    else
    {
        LOG_E("不支持的SIP协议类型!");
    }

    str = iniparser_getstring(ini, "SIP配置:本地端口", "8899");
    info->sip_local_port = std::atoi(str);

    str = iniparser_getstring(ini, "SIP配置:传输协议", "UDP");

    if (string(str) == "UDP")
    {
        info->sip_protocal_type = SIP_PROTOCAL_UDP;
    }
    else if (string(str) == "TCP")
    {
        info->sip_protocal_type = SIP_PROTOCAL_TCP;
    }
    else
    {
        LOG_E("不支持的SIP协议类型!");
    }

    str = iniparser_getstring(ini, "SIP配置:服务器ID", /*"34020000001300000001"*/ "");
    memcpy(info->sip_server_id, str, strlen(str));

    str = iniparser_getstring(ini, "SIP配置:服务器域", /*"3402000000"*/ "");
    memcpy(info->sip_server_domain, str, strlen(str));

    str = iniparser_getstring(ini, "SIP配置:服务器地址", /*"106.14.80.181"*/ "");
    ip_addr_trans(string(str), info->sip_server_ip_addr);

    str = iniparser_getstring(ini, "SIP配置:服务器端口", /*"30905"*/ "");
    info->sip_server_port = std::atoi(str);

    str = iniparser_getstring(ini, "SIP配置:用户名", /*"未命名"*/ "");
    memcpy(info->sip_user_name, str, strlen(str));

    str = iniparser_getstring(ini, "SIP配置:用户认证ID", /*"34020000001320000009"*/ "");
    memcpy(info->sip_user_id, str, strlen(str));

    str = iniparser_getstring(ini, "SIP配置:密码", /*"123456"*/ "");
    memcpy(info->sip_password, str, strlen(str));

    str = iniparser_getstring(ini, "SIP配置:注册有效期", /*"3600"*/ "");
    info->sip_register_timeout_second = std::atoi(str);

    str = iniparser_getstring(ini, "视频配置:视频宽度", /*"640"*/ "");
    info->video_iw = std::atoi(str);

    str = iniparser_getstring(ini, "视频配置:视频高度", /*"480"*/ "");
    info->video_ih = std::atoi(str);

    str = iniparser_getstring(ini, "视频配置:视频码率", /*"500"*/ "");
    info->video_encode_bps = std::atoi(str);

    str = iniparser_getstring(ini, "视频配置:视频帧率", /*"25"*/ "");
    info->video_frame_rate = std::atoi(str);

    //	"时间格式     = 12小时制  ;\n"
    //	"日期格式     = 年年-月月-日日  ;\n"

    str = iniparser_getstring(ini, "OSD配置:通道名称", /*"测试设备"*/ "");
    memcpy(info->osd_channel_name, str, strlen(str));

    str = iniparser_getstring(ini, "OSD配置:显示通道名称", /*"yes"*/ "");
    info->osd_en_channel_name = string(str) == "yes" ? 1 : 0;

    str = iniparser_getstring(ini, "OSD配置:显示日期", /*"yes"*/ "");
    info->osd_en_date_info = string(str) == "yes" ? 1 : 0;

    str = iniparser_getstring(ini, "OSD配置:显示时间", /*"yes"*/ "");
    info->osd_en_week_info = string(str) == "yes" ? 1 : 0;

    str = iniparser_getstring(ini, "OSD配置:叠加信息1", /*"测试信息1"*/ "");
    memcpy(info->osd_info[0], str, strlen(str));

    str = iniparser_getstring(ini, "OSD配置:叠加信息2", /* "测试信息2"*/ "");
    memcpy(info->osd_info[1], str, strlen(str));

    str = iniparser_getstring(ini, "OSD配置:叠加信息3", /*"测试信息3"*/ "");
    memcpy(info->osd_info[2], str, strlen(str));

    str = iniparser_getstring(ini, "OSD配置:叠加信息4", /*"测试信息4"*/ "");
    memcpy(info->osd_info[3], str, strlen(str));

    str = iniparser_getstring(ini, "OSD配置:叠加信息1坐标x", /*"0"*/ "");
    info->osd_info_location[0][0] = std::atoi(str);

    str = iniparser_getstring(ini, "OSD配置:叠加信息1坐标y", /*"0"*/ "");
    info->osd_info_location[0][1] = std::atoi(str);

    str = iniparser_getstring(ini, "OSD配置:叠加信息2坐标x", /*"0"*/ "");
    info->osd_info_location[1][0] = std::atoi(str);

    str = iniparser_getstring(ini, "OSD配置:叠加信息2坐标y", /*"0"*/ "");
    info->osd_info_location[1][1] = std::atoi(str);

    str = iniparser_getstring(ini, "OSD配置:叠加信息3坐标x", /*"0"*/ "");
    info->osd_info_location[2][0] = std::atoi(str);

    str = iniparser_getstring(ini, "OSD配置:叠加信息3坐标y", /*"0"*/ "");
    info->osd_info_location[2][1] = std::atoi(str);

    str = iniparser_getstring(ini, "OSD配置:叠加信息4坐标x", /*"0"*/ "");
    info->osd_info_location[3][0] = std::atoi(str);

    str = iniparser_getstring(ini, "OSD配置:叠加信息4坐标y", /*"0"*/ "");
    info->osd_info_location[3][1] = std::atoi(str);

    str = iniparser_getstring(ini, "抓拍配置:是否启用抓拍", "no");
    info->capture_en_start = (string(str) == "yes") ? 1 : 0;

    str = iniparser_getstring(ini, "抓拍配置:图片格式", "BMP");

    if (string(str) == "BMP")
    {
        info->image_format = IMAGE_FORMAT_BMP;
    }
    else if (string(str) == "JPEG")
    {
        info->image_format = IMAGE_FORMAT_JPEG;
    }
    else
    {
        LOG_E("不支持的图片类型！");
    }

    str = iniparser_getstring(ini, "抓拍配置:抓拍间隔", /*"2000"*/ "");
    info->capture_time_interval = std::atoi(str);

    str = iniparser_getstring(ini, "抓拍配置:抓拍间隔时间单位", /*"毫秒"*/ "");
    if (string(str) == "毫秒")
    {
        info->capture_time_unit = IMAGE_TIME_UNIT_MILLISECOND;
    }
    else if (string(str) == "秒")
    {
        info->capture_time_unit = IMAGE_TIME_UNIT_SECOND;
    }
    else if (string(str) == "分钟")
    {
        info->capture_time_unit = IMAGE_TIME_UNIT_MINUTE;
    }
    else if (string(str) == "小时")
    {
        info->capture_time_unit = IMAGE_TIME_UNIT_HOUR;
    }
    else
    {
        info->capture_time_unit = IMAGE_TIME_UNIT_DAY;
    }

    str = iniparser_getstring(ini, "抓拍配置:抓图是否保存到服务器", "yes");
    info->capture_en_save_server = (string(str) == "yes") ? 1 : 0;

    str = iniparser_getstring(ini, "抓拍配置:上传地址", /*"http//"*/ "");
    memcpy(info->capture_server_url, str, strlen(str));

    str = iniparser_getstring(ini, "抓拍配置:周一是否抓拍", "no");
    info->capture_every_week[0][0] = (string(str) == "yes") ? 1 : 0;
    str = iniparser_getstring(ini, "抓拍配置:周一抓拍起始时间", /*"0"*/ "");
    info->capture_every_week[0][1] = std::atoi(str);
    str = iniparser_getstring(ini, "抓拍配置:周一抓拍结束时间", /*"0"*/ "");
    info->capture_every_week[0][2] = std::atoi(str);
    str = iniparser_getstring(ini, "抓拍配置:周二是否抓拍", "no");
    info->capture_every_week[1][0] = (string(str) == "yes") ? 1 : 0;
    str = iniparser_getstring(ini, "抓拍配置:周二抓拍起始时间", /*"0"*/ "");
    info->capture_every_week[1][1] = std::atoi(str);
    str = iniparser_getstring(ini, "抓拍配置:周二抓拍结束时间", /*"0"*/ "");
    info->capture_every_week[1][2] = std::atoi(str);
    str = iniparser_getstring(ini, "抓拍配置:周三是否抓拍", "no");
    info->capture_every_week[2][0] = (string(str) == "yes") ? 1 : 0;
    str = iniparser_getstring(ini, "抓拍配置:周三抓拍起始时间", /*"0"*/ "");
    info->capture_every_week[2][1] = std::atoi(str);
    str = iniparser_getstring(ini, "抓拍配置:周三抓拍结束时间", /*"0"*/ "");
    info->capture_every_week[2][2] = std::atoi(str);
    str = iniparser_getstring(ini, "抓拍配置:周四是否抓拍", "no");
    info->capture_every_week[3][0] = (string(str) == "yes") ? 1 : 0;
    str = iniparser_getstring(ini, "抓拍配置:周四抓拍起始时间", /*"0"*/ "");
    info->capture_every_week[3][1] = std::atoi(str);
    str = iniparser_getstring(ini, "抓拍配置:周四抓拍结束时间", /*"0"*/ "");
    info->capture_every_week[3][2] = std::atoi(str);
    str = iniparser_getstring(ini, "抓拍配置:周五是否抓拍", "no");
    info->capture_every_week[4][0] = (string(str) == "yes") ? 1 : 0;
    str = iniparser_getstring(ini, "抓拍配置:周五抓拍起始时间", /*"0"*/ "");
    info->capture_every_week[4][1] = std::atoi(str);
    str = iniparser_getstring(ini, "抓拍配置:周五抓拍结束时间", /*"0"*/ "");
    info->capture_every_week[4][2] = std::atoi(str);
    str = iniparser_getstring(ini, "抓拍配置:周六是否抓拍", "no");
    info->capture_every_week[5][0] = (string(str) == "yes") ? 1 : 0;
    str = iniparser_getstring(ini, "抓拍配置:周六抓拍起始时间", /*"0"*/ "");
    info->capture_every_week[5][1] = std::atoi(str);
    str = iniparser_getstring(ini, "抓拍配置:周六抓拍结束时间", /*"0"*/ "");
    info->capture_every_week[5][2] = std::atoi(str);
    str = iniparser_getstring(ini, "抓拍配置:周天是否抓拍", "no");
    info->capture_every_week[6][0] = (string(str) == "yes") ? 1 : 0;
    str = iniparser_getstring(ini, "抓拍配置:周天抓拍起始时间", /*"0"*/ "");
    info->capture_every_week[6][1] = std::atoi(str);
    str = iniparser_getstring(ini, "抓拍配置:周天抓拍结束时间", /*"0"*/ "");
    info->capture_every_week[6][2] = std::atoi(str);

    str = iniparser_getstring(ini, "录像配置:是否启用录像", "no");
    info->record_en_start = (string(str) == "yes") ? 1 : 0;

    str = iniparser_getstring(ini, "录像配置:周一是否录像", "no");
    info->record_every_week[0][0] = (string(str) == "yes") ? 1 : 0;
    str = iniparser_getstring(ini, "录像配置:周一录像起始时间", /*"0"*/ "");
    info->record_every_week[0][1] = std::atoi(str);
    str = iniparser_getstring(ini, "录像配置:周一录像结束时间", /*"0"*/ "");
    info->record_every_week[0][2] = std::atoi(str);
    str = iniparser_getstring(ini, "录像配置:周二是否录像", "no");
    info->record_every_week[1][0] = (string(str) == "yes") ? 1 : 0;
    str = iniparser_getstring(ini, "录像配置:周二录像起始时间", /*"0"*/ "");
    info->record_every_week[1][1] = std::atoi(str);
    str = iniparser_getstring(ini, "录像配置:周二录像结束时间", /*"0"*/ "");
    info->record_every_week[1][2] = std::atoi(str);
    str = iniparser_getstring(ini, "录像配置:周三是否录像", "no");
    info->record_every_week[2][0] = (string(str) == "yes") ? 1 : 0;
    str = iniparser_getstring(ini, "录像配置:周三录像起始时间", /*"0"*/ "");
    info->record_every_week[2][1] = std::atoi(str);
    str = iniparser_getstring(ini, "录像配置:周三录像结束时间", /*"0"*/ "");
    info->record_every_week[2][2] = std::atoi(str);
    str = iniparser_getstring(ini, "录像配置:周四是否录像", "no");
    info->record_every_week[3][0] = (string(str) == "yes") ? 1 : 0;
    str = iniparser_getstring(ini, "录像配置:周四录像起始时间", /*"0"*/ "");
    info->record_every_week[3][1] = std::atoi(str);
    str = iniparser_getstring(ini, "录像配置:周四录像结束时间", /*"0"*/ "");
    info->record_every_week[3][2] = std::atoi(str);
    str = iniparser_getstring(ini, "录像配置:周五是否录像", "no");
    info->record_every_week[4][0] = (string(str) == "yes") ? 1 : 0;
    str = iniparser_getstring(ini, "录像配置:周五录像起始时间", /*"0"*/ "");
    info->record_every_week[4][1] = std::atoi(str);
    str = iniparser_getstring(ini, "录像配置:周五录像结束时间", /*"0"*/ "");
    info->record_every_week[4][2] = std::atoi(str);
    str = iniparser_getstring(ini, "录像配置:周六是否录像", "no");
    info->record_every_week[5][0] = (string(str) == "yes") ? 1 : 0;
    str = iniparser_getstring(ini, "录像配置:周六录像起始时间", /*"0"*/ "");
    info->record_every_week[5][1] = std::atoi(str);
    str = iniparser_getstring(ini, "录像配置:周六录像结束时间", /*"0"*/ "");
    info->record_every_week[5][2] = std::atoi(str);
    str = iniparser_getstring(ini, "录像配置:周天是否录像", "no");
    info->record_every_week[6][0] = (string(str) == "yes") ? 1 : 0;
    str = iniparser_getstring(ini, "录像配置:周天录像起始时间", /*"0"*/ "");
    info->record_every_week[6][1] = std::atoi(str);
    str = iniparser_getstring(ini, "录像配置:周天录像结束时间", /*"0"*/ "");
    info->record_every_week[6][2] = std::atoi(str);

    iniparser_freedict(ini);

    // utf8-->gb2312
    // info->device_name;
    // info->osd_channel_name;
    // info->osd_info;
    utf8_to_gb2312(info->device_name);
    utf8_to_gb2312(info->osd_channel_name);
    for (int i = 0; i < 4; ++i)
    {
        utf8_to_gb2312(info->osd_info[i]);
    }

    return 0;
}

/**
 * UTF-8 转 GB2312，直接覆盖原缓冲区（128字节）
 * @param buff: 输入UTF-8字符串，输出GB2312字符串（均以'\0'结尾）
 * @return 转换结果（CONVERT_SUCCESS 为成功）
 */
int utf8_to_gb2312(uint8_t buff[128])
{
    // 检查输入合法性
    if (!buff || buff[0] == '\0')
    {
        return CONVERT_NULL_INPUT;
    }

    // 创建临时缓冲区（避免转换过程中覆盖输入数据）
    uint8_t temp_buff[128] = {0};
    iconv_t cd = iconv_open("GB2312//IGNORE", "UTF-8");
    if (cd == (iconv_t)-1)
    {
        return CONVERT_FAIL;
    }

    // 准备转换参数（iconv会修改指针，需用临时变量）
    char *in_ptr = (char *)buff;
    size_t in_len = strlen((char *)buff);
    char *out_ptr = (char *)temp_buff;
    size_t out_len = 127; // 预留1字节给终止符

    // 执行转换
    size_t ret = iconv(cd, &in_ptr, &in_len, &out_ptr, &out_len);
    iconv_close(cd);

    // 检查转换结果
    if (ret == (size_t)-1)
    {
        return CONVERT_FAIL;
    }

    // 检查是否溢出（剩余空间是否足够容纳终止符）
    if (out_len == 0)
    {
        return CONVERT_BUFFER_OVERFLOW;
    }

    // 覆盖原缓冲区
    memset(buff, 0, 128);
    memcpy(buff, temp_buff, 127); // 最多复制127字节，确保最后1字节为'\0'
    buff[127] = '\0';             // 强制终止符，防止溢出

    return CONVERT_SUCCESS;
}

/**
 * GB2312 转 UTF-8，直接覆盖原缓冲区（128字节）
 * @param buff: 输入GB2312字符串，输出UTF-8字符串（均以'\0'结尾）
 * @return 转换结果（CONVERT_SUCCESS 为成功）
 */
int gb2312_to_utf8(uint8_t buff[128])
{
    // 检查输入合法性
    if (!buff || buff[0] == '\0')
    {
        return CONVERT_NULL_INPUT;
    }

    // 创建临时缓冲区（避免转换过程中覆盖输入数据）
    uint8_t temp_buff[128] = {0};
    iconv_t cd = iconv_open("UTF-8//IGNORE", "GB2312");
    if (cd == (iconv_t)-1)
    {
        return CONVERT_FAIL;
    }

    // 准备转换参数
    char *in_ptr = (char *)buff;
    size_t in_len = strlen((char *)buff);
    char *out_ptr = (char *)temp_buff;
    size_t out_len = 127; // 预留1字节给终止符

    // 执行转换
    size_t ret = iconv(cd, &in_ptr, &in_len, &out_ptr, &out_len);
    iconv_close(cd);

    // 检查转换结果
    if (ret == (size_t)-1)
    {
        return CONVERT_FAIL;
    }

    // 检查是否溢出
    if (out_len == 0)
    {
        return CONVERT_BUFFER_OVERFLOW;
    }

    // 覆盖原缓冲区
    memset(buff, 0, 128);
    memcpy(buff, temp_buff, 127);
    buff[127] = '\0'; // 强制终止符

    return CONVERT_SUCCESS;
}